#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#include "c_pso.h"

#define IT 100000

int main(int argc, char * argv[])
{
	pso_swarm swarms[N_SWARMS];
	int j,k;
	long long int i;
	double complete = 0;

	srand(time(NULL));

	for (i = 0; i < N_SWARMS; i++) {
		init_swarm(swarms + i);
	}

/*
	for (j = 0; j < N; j++) {
		printf("Particle: %d - ", j);
		for (i = 0; i < DIM; i++) {
			printf("%4.2lf ",(double)(swarms->part_swarm[j].location[i]));
		}
		printf("%4.2lf",(double)(swarms->part_swarm[j].fitness));
		printf("\n");
	}
*/

	printf("               ");
	for (i = 0; i < IT; i++) {
		if (i > (complete * IT)) {
			for (j = 0; j < 16; j++) printf("\b");
			if (complete < .1) printf(" ");
			printf("%3.1lf%% Complete ", complete * 100);
			fflush(stdout);
			complete += .005;
		}
		for (j = 0; j < N_SWARMS; j++) {
			calc_fitness_swarm(swarms + j);
			move_swarm(swarms + j);
		}

		for (j = 0; j < N_SWARMS; j++) {
			for (k = 0; k < N_SWARMS; k++) {
				if (frand() < CROSS_RATE) {
					swap_part(swarms + j, swarms + k);
				}
			}
		}
	}
	for (j = 0; j < 16; j++) printf("\b");
	printf("%lf%% Complete\n", 100.0);

	for (j = 0; j < N_SWARMS; j++) {
		printf("Swarm: %d - ", j + 1);
		for (i = 0; i < DIM; i++) {
			printf("%4.2lf ",(double)(swarms[j].best->location[i]));
		}
		printf("%4.2lf ",(double)(swarms[j].best->fitness));
		printf("\n");
	}

	return 0;
}
